package com.zygh.interactiveplatform.module.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.interactiveplatform.common.constant.EmployeeResponseCodeConst;
import com.zygh.interactiveplatform.common.domain.ResponseDTO;
import com.zygh.interactiveplatform.module.entity.Dept;
import com.zygh.interactiveplatform.module.entity.DirectionCode;
import com.zygh.interactiveplatform.module.entity.User;
import com.zygh.interactiveplatform.module.mapper.DeptMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zygh.interactiveplatform.module.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lee
 * @since 2021-01-28
 */
@Service
public class DeptService extends ServiceImpl<DeptMapper, Dept> {
    @Resource
    private DeptMapper deptMapper;
    @Resource
    private UserMapper userMapper;


    public ResponseDTO addDept(Dept dept) {
        int insert = deptMapper.insert(dept);
        if (insert > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.DEPT_ADD_EXIST);
    }


    public ResponseDTO updateDept(Dept dept) {
        int i = deptMapper.updateById(dept);
        if (i > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.DEPT_UPDATE_EXIST);
    }


    public ResponseDTO deleteDept(Integer id) {
        int i = deptMapper.deleteById(id);
        if (i > 0) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.DEPT_DELETE_EXIST);
    }


    public ResponseDTO findDeptAll(Integer current, Integer size) {
        PageHelper.startPage(current, size);
        List<Dept> depts = deptMapper.findAll();
        if (depts != null) {
            for (int i = 0; i < depts.size(); i++) {
             /*   List<User> users = userMapper.selectList(Wrappers.lambdaQuery(User.class)
                        .eq(User::getDeptId, depts.get(i).getDeptId()));*/
                List<User> userByDeptId = userMapper.findUserByDeptId(depts.get(i).getDeptId());
                depts.get(i).setUserList(userByDeptId);
            }
            PageInfo<Dept> pageInfo = new PageInfo<>(depts);
            return ResponseDTO.succData(pageInfo);
        }
        return ResponseDTO.wrap(EmployeeResponseCodeConst.EMPTY_INFORMATION_ERROR);
    }


}
